package com.example.farmer.Fragment.Message;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.farmer.Entity.ChatMessage;
import com.example.farmer.R;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class ChatMessageAdapter extends BaseAdapter {

    private Context context;
    private List<ChatMessage> messages;
    private TextView messageTextView;
    private TextView timestampTextView;

    public ChatMessageAdapter(Context context, List<ChatMessage> messages) {
        this.context = context;
        this.messages = messages;
    }

    @Override
    public int getCount() {
        return messages.size();
    }

    @Override
    public Object getItem(int position) {
        return messages.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.chat_detail_item, parent, false);
        }

//        TextView senderTextView = convertView.findViewById(R.id.user_number);
         messageTextView = convertView.findViewById(R.id.xinxi);
         timestampTextView = convertView.findViewById(R.id.timestamp);

        ChatMessage message = messages.get(position);
        messageTextView.setText(message.getMessageContent());
        // 假设有一个方法来格式化时间戳
        String timestamp = new SimpleDateFormat("HH:mm:ss", Locale.getDefault()).format(new Date());; // 这里应该是格式化后的时间戳
        timestampTextView.setText(timestamp);

        return convertView;
    }

}
